--[[ 		     SLDataText Module: Clock 				]]
--[[ Author: Taffu  RevDate: 07/13/2012  Version: 5.0.0 ]]

-- Fixed time formatting in tooltip

local SLDT, MODNAME = SLDataText, "Clock"
if ( SLDT ) then SLDT.Clock = CreateFrame("Frame") end
local L = SLDT.Locale
local db, frame, text, tool

local rCol, gCol = RED_FONT_COLOR_CODE, GREEN_FONT_COLOR_CODE
local locTime, rlmTime
local function getGameTime()
    local serTime, serAMPM
    local hour, minu = GetGameTime()
	if ( minu < 10 ) then minu = string.format("0%i", minu) end 
    if ( db.format24 ) then
        serAMPM = ""; serTime = string.format("%s:%s", hour, minu)
    else
        if ( hour > 11 ) then serAMPM = "pm" else serAMPM = "am" end
        if ( hour == 0 ) then hour = hour + 12 elseif hour > 12 then hour = hour - 12 end
        serTime = string.format("%s:%s", hour, minu)
    end
    return serTime, serAMPM
end

local function formatTimer(wait, long)
	local timestring
	local h, m, s = 0, 0, 0
	if ( wait >= 3600 ) then
		h = floor(wait / 3600); wait = wait - (h * 3600)
		m = floor(wait / 60); wait = wait - (m * 60)
		s = floor(wait)
		
		if ( m < 10 ) then m = string.format("%d%d", 0, m) end
		if ( s < 10 ) then s = string.format("%d%d", 0, s) end
		
		if ( long ) then
			timestring = string.format("%s hours", h)
		else
			timestring = string.format("%s:%s:%s", h, m, s)
		end
	elseif ( wait >= 60 and wait < 3600 ) then
		m = floor(wait / 60); wait = wait - (m * 60)
		s = floor(wait)
		
		if ( s < 10 ) then s = string.format("%d%d", 0, s) end
		
		if ( long ) then
			timestring = string.format("%s min %s sec", m, s)
		else
			timestring = string.format("%s:%s", m, s)
		end
	elseif ( wait < 60 ) then
		s = floor(wait)
		
		if ( long ) then
			timestring = string.format("%s sec", s)
		else
			timestring = string.format("%s", s)
		end
	end
	
	return timestring
end

local function SetupToolTip()
	tool:SetScript("OnEnter", function(this)
		GameTooltip:SetOwner(this, db.aF)
		GameTooltip:AddLine(string.format("|cffffffff%s %i, %i|r", date("%B"), date("%d"), date("%Y")))
		GameTooltip:AddDoubleLine("Realm Time", string.format("%s%s", select(1, getGameTime()), select(2, getGameTime())), 1,1,0,1,1,1)
		local hr, mi, mer = date("%I"), date("%M"), string.lower(date("%p"))
		GameTooltip:AddDoubleLine("Local Time", string.format("%i:%s%s", hr, mi, mer), 1,1,0,1,1,1)
		GameTooltip:AddLine(" ")
		
		if ( db.showPVP ) then
			local inQueue = nil
			for i = 1, 2 do
				local id, name, active, canQ, wait, _ = GetWorldPVPAreaInfo(i)
				if ( active ) then
					name = string.format("%s%s (A)|r", rCol, name)
				elseif ( canQ ) then
					name = string.format("%s%s (Q)|r", gCol, name)
				end
				GameTooltip:AddDoubleLine(name, formatTimer(wait))
			end
			
			for j = 1, MAX_WORLD_PVP_QUEUES do
				local status, map = GetWorldPVPQueueStatus(j)
				if ( status == "queued" ) then inQueue = {}; table.insert(inQueue, map) end
			end
			
			if ( inQueue ~= nil ) then
				local maps = ""
				for k, v in pairs(inQueue) do
					maps = string.format("%s %s", maps, v)
				end
				GameTooltip:AddLine(string.format("%s %s", L["Queued for:"], maps))
			end
			
			GameTooltip:AddLine(" ")
		end
		
		GameTooltip:AddDoubleLine(L["Left-Click"], L["Toggle Calendar"], 1,1,0,1,1,1)
		GameTooltip:AddDoubleLine(L["Right-Click"], L["Toggle Time Manager"], 1,1,0,1,1,1)
		GameTooltip:Show()
	end)
	tool:SetScript("OnLeave", function(this)
		if ( GameTooltip:IsShown() ) then GameTooltip:Hide() end
	end)
	tool:SetScript("OnMouseDown", function(self, button)
		if ( button == "LeftButton" ) then 
			ToggleCalendar()
		elseif ( button == "RightButton" ) then
			ToggleTimeManager()
		end
	end)
end

function SLDT.Clock:Enable()
	if ( db.enabled ) then
		SLDT:UpdateBaseText(self, db)
		frame:SetScript("OnUpdate", function(_, elapsed)
			self:Refresh()
		end)
	end
end

function SLDT.Clock:Disable()
	if ( not db.enabled ) then
		frame:SetScript("OnUpdate", nil)
	end
	self:Refresh()
end

function SLDT.Clock:Refresh()
	if ( db.enabled or SLDT.db.profile.configMode ) then
		if ( not self.firstRun ) then self.firstRun = true; SLDT:UpdateBaseText(self, db) end
		
		if ( TimeManagerClockButton:IsShown() ) then TimeManagerClockButton:Hide() end
		rlmTime = string.format("%s|cff%s%s|r", select(1, getGameTime()), SLDT.db.profile.cCol and SLDT.classColor or "ffffff", select(2, getGameTime()))
		
		local timeString = date(db.timeString)
		if ( db.hourCorrect and string.sub(timeString, 0, 1) == "0" ) then
			timeString = string.sub(timeString, 2, string.len(timeString))
		end
		
		text:SetText(db.realmTime and rlmTime or timeString)
		
        local invites = CalendarGetNumPendingInvites()
		if ( invites > 0 ) then SLDT:Pulse(self, true) else SLDT:Pulse(self, false) end
		
		SLDT:UpdateBaseFrame(self, db)
	else
		if ( not TimeManagerClockButton:IsShown() ) then TimeManagerClockButton:Show() end
		if ( frame:IsShown() and not SLDataText.db.profile.configMode ) then frame:Hide() end
	end
end

SLDT.Clock.optsTbl = {
	[1] = { [1] = "toggle", [2] = L["Enabled"], [3] = "enabled" },
	[2] = { [1] = "toggle", [2] = L["Global Font"], [3] = "gfont" },
	[3] = { [1] = "toggle", [2] = L["Outline"], [3] = "outline" },
	[4] = { [1] = "toggle", [2] = L["Force Shown"], [3] = "forceShow" },
	[5] = { [1] = "toggle", [2] = L["Tooltip On"], [3] = "tooltipOn" },
	[6] = { [1] = "toggle", [2] = L["Realm Time"], [3] = "realmTime" },
	[7] = { [1] = "toggle", [2] = L["PvP Info"], [3] = "showPVP" },
	[8] = { [1] = "toggle", [2] = L["CorrHour"], [3] = "hourCorrect" },
	[9] = { [1] = "text", [2] = L["Time String"], [3] = "timeString" },
	[10] = { [1] = "desc", [2] = "ClockDesc", [3] = L["ClockDesc"] },
	[11] = { [1] = "range", [2] = L["Font Size"], [3] = "fontSize", [4] = 6, [5] = 40, [6] = 1 },
	[12] = { [1] = "select", [2] = L["Font"], [3] = "font", [4] = SLDT.fontTbl },
	[13] = { [1] = "select", [2] = L["Justify"], [3] = "aP", [4] = SLDT.justTbl },
	[14] = { [1] = "text", [2] = L["Parent"], [3] = "anch" },
	[15] = { [1] = "select", [2] = L["Anchor"], [3] = "aF", [4] = SLDT.anchTbl },
	[16] = { [1] = "text", [2] = L["X Offset"], [3] = "xOff" },
	[17] = { [1] = "text", [2] = L["Y Offset"], [3] = "yOff" },
	[18] = { [1] = "select", [2] = L["Frame Strata"], [3] = "strata", [4] = SLDT.stratTbl },
}

local function OnInit()
	SLDT.Clock.db = SLDT.db:RegisterNamespace(MODNAME)
    SLDT.Clock.db:RegisterDefaults({
        profile = {
			name = "Clock",
			enabled = true,
			forceShow = true,
			aP = "CENTER",
			anch = "Minimap",
			aF = "BOTTOM",
			xOff = 0,
			yOff = -10,
			strata = "MEDIUM",
			gfont = false,
			realmTime = false,
			fontSize = 12,
			font = "Arial Narrow",
			outline = false,
			tooltipOn = true,
			showPVP = true,
			timeString = "%I:%M:%S%p",
			hourCorrect = true,
        },
    })
	db = SLDT.Clock.db.profile
	
	SLDT.Modules = SLDT.Modules or {}
	if ( not SLDT.Modules[MODNAME] ) then table.insert(SLDT.Modules, { MODNAME, db }) end
	frame, text, tool = SLDT:SetupBaseFrame(SLDT.Clock)
	SetupToolTip()
	
	SLDT.Clock:UnregisterEvent("PLAYER_ENTERING_WORLD")
	SLDT.Clock:Enable()
end

SLDT.Clock:RegisterEvent("PLAYER_ENTERING_WORLD")
SLDT.Clock:SetScript("OnEvent", OnInit)